package collections;

import java.util.Collections;
import java.util.HashMap;

/**
 * Created by pc on 2015/2/3.
 * 笨办法，把数组元素依次放入HashMap，并统计出现次数
 * 为出现的次数排序，找出最多的出现次数
 * 然后找出等于最多出现次数的对应的元素*
 */
public class FindDuplicate {
    int[] checkArr = new int[]{34, 53, 23, 34, 2, 45, 53, 53, -9, 34};

    public void findMaxDupEle() {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int ele : checkArr) {
            if (!map.containsKey(ele)) {
                map.put(ele, 1);
            } else {
                int count = map.get(ele);
                map.put(ele, ++count);
            }
        }
        int highest =  Collections.max(map.values());
        System.out.println("max value is: " + highest);
        for (Integer keyEle : map.keySet()){
            int count = map.get(keyEle);
            if (count == highest) {
                System.out.println("出现次数最多的数是： " + keyEle.toString()  );
            }
        }
//        for (Integer keyEle : map.keySet()){
//            list.add(map.get(keyEle));
//
//            int count = map.get(keyEle);
//            if (count >= highest) {
//                highest = count;
//            }
//            System.out.println(keyEle.toString() + " ==> " + count );
//        }

//        System.out.println("highest value is: " + highest);
        
//        Collections.sort(list);
//        System.out.println("after sort:" + list);
//        System.out.println();
//        Collections.sort();
//        ArrayList<Integer> cArr = new ArrayList<Integer>(countArr.size());
//        for (int i=0; i<countArr.size(); i++){
//            cArr[i]= countArr[i];
//        }
//        Collections.sort(cArr);

    }
    
    public static void main(String args[]){
        FindDuplicate fd = new FindDuplicate();
        fd.findMaxDupEle();
    }
}

